package com.syni.mdd.yhd.common.all.controller.api.sms;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.service.BmsUserService;
import com.syni.mdd.yhd.common.all.utils.SmsUtils;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiUser.GetIdentifyingCodeDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.SmsRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Action;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @className SmsRestApi
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/4 10:51
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/sms")
public class SmsRestApi implements SmsRestService {

    @Autowired
    BmsUserService bmsUserService;

    @PostMapping("/mddSend")
    public Map<String,Object> send(@RequestBody ParamsMap paramsMap){
        String templateId = paramsMap.getString("templateId");
        String phone = paramsMap.getString("phone");
        String params = paramsMap.getString("params");
        List<String> paramsList = JSON.parseArray(params,String.class);
        String[] paramsArray = new String[paramsList.size()];
        paramsList.toArray(paramsArray);
        Integer from = paramsMap.getInteger("from");
        String client = "user";
        if(from == Constants.BUSINESS_CLIENT){
            client = "business";
        }
        if(!SmsUtils.thirdPartySend(phone,templateId,paramsArray,client)) {
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100, "发送失败");
        }
        return ResultMapHelper.success(null, "发送成功");
    }

    @PostMapping("/localLifeSend")
    public Map<String,Object> localLifeSend(@RequestBody ParamsMap paramsMap){
        String content = paramsMap.getString("content");
        String phone = paramsMap.getString("phone");
        String templateIds = "localLife.tip";
        String[] params = {
                "觅东东",content};
        SmsUtils.send(phone, templateIds, params);
        return ResultMapHelper.success(null,"发送成功");
    }

    @PostMapping("/businessMerchantsSend")
    public Map<String,Object> businessMerchantsSend(@RequestBody ParamsMap paramsMap){
        String content = paramsMap.getString("content");
        String phone = paramsMap.getString("phone");
        String templateIds = paramsMap.getString("templateId");
        List<String> params = JSON.parseArray(content,String.class);
        String[] sendParams = params.toArray(new String[params.size()]);
        SmsUtils.customSend(phone, templateIds, sendParams);
        return ResultMapHelper.success(null,"发送成功");
    }




    @PostMapping("/getIdentifyingCode")
    public RpcResponseVo getIdentifyingCode(@RequestBody GetIdentifyingCodeDto getIdentifyingCodeDto) {
        Map<String,Object> retMap = bmsUserService.sendUserIdentifingCode(getIdentifyingCodeDto);
        if(ResultMapHelper.isSuccess(retMap)){
            return RpcResponseVo.success(retMap.get("data"), ResultMsg.MSG_GET_SUCCESS);
        }
        return RpcResponseVo.result(retMap.get("data").toString(),retMap.get("msg").toString());
    }




}
